package com.hc.android.research.component;

import java.util.ArrayList;
import java.util.List;

import com.hc.android.research.R;
import com.hc.android.research.dom.ItemPrice;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ItemViewAdapter extends ArrayAdapter<ItemView> 
{

	private final Activity activity;
	private final List<ItemView> items;
	
	public ItemViewAdapter(Activity activity, List<ItemView> objects)
	{
		super(activity, R.layout.itemview, objects);
		items = objects;
		this.activity = activity;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	
		View rowView = convertView;
		Item itemView = null;
		
		if(rowView == null)
		{
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.itemview, null);
				
			itemView = new Item();
			itemView.urlTxt = (TextView) rowView.findViewById(R.id.urlTxt);
			itemView.dataTxt = (TextView) rowView.findViewById(R.id.dataTxt);
			itemView.historyList = (ListView) rowView.findViewById(R.id.historyList);
			
			rowView.setTag(itemView);
		}
		else
		{
			itemView = (Item) rowView.getTag();
		}
		
		ItemView currentItem = items.get(position);
		itemView.dataTxt.setText("");
		itemView.urlTxt.setText(currentItem.getItem().getItemName() + " - " + currentItem.getItem().getItemURL().toString());
		List<TextView> textViewList = new ArrayList<TextView>();
		List<ItemPrice> prices = currentItem.getItem().getPrices();
		if(prices != null)
		{
			for(int i = 0; i < prices.size(); i++)
			{
				TextView temp = new TextView(getContext());
				temp.setText(prices.get(i).getItemPrice() + " - " + prices.get(i).getPriceDate());
				textViewList.add(temp);
			}
		}
		itemView.historyList.setAdapter(new ArrayAdapter<TextView>(getContext(), R.id.smallTextView, textViewList));
		
		
		return rowView;
	}
	
	protected static class Item
	{
		protected TextView urlTxt;
		protected TextView dataTxt;
		protected ListView historyList;
	}
}
